package com.nali.dal.statement.function.formatter;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.nali.dal.statement.function.DataHandler;
import com.nali.dal.statement.function.FunctionArgument;
import com.nali.dal.statement.function.FunctionException;
import com.nali.dal.statement.util.DalCollectionUtils;

@Component
public class ToMap implements DataHandler<Map<Object,Object>> {

	@Override
	public Map<Object,Object> handle(List<Object> objs) throws FunctionException {
		Object key=null;
		Map<Object,Object> rlt = DalCollectionUtils.newHashMap(objs.size()/2);
		for (int i = 0; i < objs.size(); i++) {
			Object obj = objs.get(i);
			if(key==null) {
				key=obj;
			} else {
				rlt.put(key, obj);
				key=null;
			}
		}
		return rlt;
	}

	@Override
	public void validate(List<FunctionArgument> arguments) throws FunctionException {
		if(arguments.size()==0) {
			throw new FunctionException("Datahandler: " + this.getClass() + ",argumetns' size is 0");
		}
		if(arguments.size()%2==1) {
			throw new FunctionException("Datahandler: " + this.getClass() + ",argumetns' size %2 is 1");
		}
	}
}

